<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/11
 * Time: 13:52
 */

namespace app\admin\logic;


use app\common\model\mysql\AuthRule;

class MenuLogic extends BaseLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new AuthRule();
    }

    public function saveData($data)
    {
        $res = $this->model->saveData($data);
        if ($res === false) {
            throw new \think\Exception("添加失败");
        }

        return true;
    }

    /**
     * @param $id
     * @param $data
     * @return bool|int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateDataById($id, $data)
    {
        $res = $this->model->updateDataById($id, $data);
        if (false === $res) {
            throw new \think\Exception("编辑失败");
        }

        return true;
    }

    public function deleteById($id)
    {
        $son = $this->model->where('pid', $id)->count();
        if ($son > 0) {
            throw new \think\Exception("该权限下有子权限，无法删除");
        }
        $res = $this->model->deleteById($id);
        if (false === $res) {
            throw new \think\Exception("权限删除失败");
        }

        return true;
    }
}